package com.yumi.controller;

import com.yumi.common.Result;
import com.yumi.entity.Employee;
import com.yumi.service.employeeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

@Tag(name = "员工管理", description = "员工信息的增删改查接口")
@RestController
@Slf4j
@RequestMapping("/api/employee")
public class EmployeeController {

    @Autowired
    private employeeService employeeService;

    /**
     * 新增员工
     * @param employee 员工信息
     * @return 操作结果
     */
    @Operation(summary = "新增员工", description = "创建新的员工信息")
    @PostMapping
    public Result insert(@RequestBody Employee employee){
        log.info("开始新增员工数据{}",employee);
        employeeService.inert(employee);
        return Result.success();
    }

    /**
     * 修改员工信息
     * @param id 员工ID
     * @param employee 员工信息
     * @return 修改后的员工信息
     */
    @Operation(summary = "修改员工信息", description = "根据ID更新员工信息")
    @PutMapping("/{id}")
    public Result<Employee> update(@PathVariable Long id,@RequestBody Employee employee) {
        log.info("修改员工信息：id={}, employee={}", id, employee);
        Employee updatedEmployee = employeeService.update(id, employee);
        return Result.success(updatedEmployee);
    }

    /**
     * 删除员工信息
     * @param id 员工ID
     * @return 操作结果
     */
    @Operation(summary = "删除员工信息", description = "根据ID删除员工信息")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        log.info("开始删除id为{}的员工信息",id);
        employeeService.delete(id);
        return Result.success();
    }

    /**
     * 分页查询员工
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 分页结果
     */
    @Operation(summary = "分页查询员工", description = "分页查询员工列表信息")
    @GetMapping("/page")
    public Result<Page<Employee>> pageQuery(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询员工信息，页码：{}，每页数量：{}", pageNum, pageSize);
        Page<Employee> page = employeeService.pageQuery(pageNum, pageSize);
        return Result.success(page);
    }
}